﻿// DigitalDesign
// Project: InformationBoard
// Developer: Syzdykov Sergey
// Created: 13.11.2011
// Last change: 16.11.2011
// Description: methods of statons page.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using GUIAdmin.AdminServiceReference;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;

namespace GUIAdmin.Views
{
    public partial class Stations : Page
    {
        //это здесь за тем, чтоб вызывать отсюда метод обновления списка вокзалов в шапке
        //по-другому не придумал
        static public MainPage mainPage { get; set; } 
        private AdminServiceClient adminClient;
        public static ObservableCollection<StationNotEF> stationList { get; set; }
        private int selectedIndex;

        public Stations()
        {
            InitializeComponent();
            adminClient = new AdminServiceClient();
            StationGrid.ItemsSource = stationList;            
            adminClient.GetListOfStationsCompleted += FillStationGrid;
            adminClient.AddStationCompleted += updateStationList;
            adminClient.UpdateStationCompleted += updateStationList;
            adminClient.DeleteStationCompleted += updateStationList;
        }
        // Выполняется, когда пользователь переходит на эту страницу.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ChangeStation.Visibility = Visibility.Collapsed;
        }
        #region buttonClick

        private void StationGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            showChangePanel();
        }         


        private void AddStation_Click(object sender, RoutedEventArgs e)
        {
            showAddPanel();
        }

        private void Create_Click(object sender, RoutedEventArgs e)
        {
            if (StationName.Text != "")
            {
                StationNotEF stationNotEF = new StationNotEF();
                stationNotEF.Name = StationName.Text;
                stationNotEF.Address = StationAddress.Text;
                adminClient.AddStationAsync(stationNotEF);
                selectedIndex = -1;
                StationName.Focus();
            }
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            if (StationGrid.SelectedItem != null && StationName.Text != "")
            {
                StationNotEF stationNotEF = new StationNotEF();
                stationNotEF.Name = StationName.Text;
                stationNotEF.Address = StationAddress.Text;
                stationNotEF.Id = (StationGrid.SelectedItem as StationNotEF).Id;
                stationNotEF.LocationsNotEF = (StationGrid.SelectedItem as StationNotEF).LocationsNotEF;
                adminClient.UpdateStationAsync(stationNotEF);
                StationName.Focus();
            }
        }

        private void DeleteStation_Click(object sender, RoutedEventArgs e)
        {
            if (StationGrid.SelectedIndex != -1)
            {
                adminClient.DeleteStationAsync((StationGrid.SelectedItem as StationNotEF).Id);
            }

        }

        #endregion

        #region clientEvent
        private void FillStationGrid(object sender, GetListOfStationsCompletedEventArgs e)
        {
            int selectedIndexMainPage = mainPage.StationsSelect.SelectedIndex;
            
            stationList.Clear();
            foreach (var station in e.Result)
            {
                stationList.Add(station);
            }

            if (mainPage.StationsSelect.Items.Count != 0 && selectedIndexMainPage != -1)
            {
                if (mainPage.StationsSelect.Items.Count < selectedIndexMainPage + 1)
                    mainPage.StationsSelect.SelectedIndex = mainPage.StationsSelect.Items.Count - 1;
                else
                    mainPage.StationsSelect.SelectedIndex = selectedIndexMainPage;
            }

            if (stationList.Count != 0 && selectedIndex != -1)
            {
                if (stationList.Count < selectedIndex + 1)
                    StationGrid.SelectedIndex = stationList.Count - 1;
                else
                    StationGrid.SelectedIndex = selectedIndex;
            }
            else
            {
                showAddPanel();
            }
        }

        private void updateStationList(object sender, AsyncCompletedEventArgs e)
        {
            adminClient.GetListOfStationsAsync();
        }


        #endregion

        #region other

        private void showChangePanel()
        {
            Save.Visibility = Visibility.Visible;
            Create.Visibility = Visibility.Collapsed;
            ChangeStation.Visibility = Visibility.Visible;
            if (StationGrid.SelectedItem != null)
            {
                StationName.Text = (StationGrid.SelectedItem as StationNotEF).Name;
                StationAddress.Text = (StationGrid.SelectedItem as StationNotEF).Address;
                selectedIndex = StationGrid.SelectedIndex;
                StationName.Focus();
            }
        }

        private void showAddPanel()
        {
            Save.Visibility = Visibility.Collapsed;
            Create.Visibility = Visibility.Visible;
            ChangeStation.Visibility = Visibility.Visible;
            StationName.Text = "";
            StationAddress.Text = "";
            StationName.Focus();
        }
        #endregion


    }
}
